clear all;close all;clc
% in this study, we will keep the target in the -60 degree, and the noise
% (reversed sentences) in the +60 degree.
x_loc = 0:-5:-355; %We would like to use -60 and +60. So we choose 
y_loc = [-57,-30,-15,0,15,30,45,60,75];
tar_index = 13;
noise_index = 61;
Elevation = 4; % ear level, 4
x_loc(tar_index)
x_loc(noise_index)
fs = 32.1e3; % this is the one for the 3D3A database
out_hart = ncread('Acoustic/Subject1/Subject1_HRIRs.sofa',"Data.IR");
for igroup = 1:8 % 8 groups will make 200 sentences
    igroup
    target = [];
    noise = [];
    randNoise = randperm(25);
    for isent = 1:25
        if isent<10
            [ori,fs2]=audioread(['Speech_sound\BEL_SR\BEL_SR0' num2str(igroup) '0' num2str(isent) '.wav']);
        else
            [ori,fs2]=audioread(['Speech_sound\BEL_SR\BEL_SR0' num2str(igroup) num2str(isent) '.wav']);
        end
        target = [target;resample(ori,fs,fs2)];
        if randNoise(isent)<10
            [BB,fs2]=audioread(['Speech_sound\BEL_SR\BEL_SR0' num2str(igroup) '0' num2str(randNoise(isent)) '.wav']);
        else
            [BB,fs2]=audioread(['Speech_sound\BEL_SR\BEL_SR0' num2str(igroup) num2str(randNoise(isent)) '.wav']);
        end
        noise = [noise; BB];
    end
    noise = [noise; BB];
    noise_reversed = flipud(noise);

    % add spatial properties
    Index = (Elevation - 1)*72+tar_index;%
    h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
    h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs
    new_tar(:,1) = conv(target,h_l,'same');
    new_tar(:,2) = conv(target,h_r,'same');

    Index = (Elevation - 1)*72+noise_index;%
    h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
    h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs
    new_noise(:,1) = conv(noise_reversed,h_l,'same');
    new_noise(:,2) = conv(noise_reversed,h_r,'same');
    Combined_dual = (new_tar+new_noise(1:length(target),:))./2; % /2 is to make it softer
    Combined_mono = sum(Combined_dual,2);
    % sound(Combined_dual,fs)
    sound(Combined_mono,fs)
    pause(4)

audiowrite(['Spatial_sound\saved_Control_Group' num2str(igroup) '.wav'],Combined_mono,fs)
clear new_noise new_tar
end



% % out_hart = ncread(['Acoustic/Subject10/Subject10_BIRs.sofa'],"Data.IR");
%
% x_loc = 0:5:355;
% for hori = 1:5:72
%     x_loc(hori)
%     for eli = 1:1:9
%         % for Index = 1:1:648
%         Index = (eli - 1)*72+hori;
%
%         h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
%         h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs
%
%         h_l=resample(h_l,44.1e3, 32.1e3);
%         h_r=resample(h_r,44.1e3, 32.1e3);
%         % figure
%         plot(h_l,'b');hold on;plot(h_r,'r')
%         xlim([200 400])
%         title(['Hori = ' num2str(x_loc(hori))])
%
%
%         %     BB = randn(1,round(dur*fs));
%         fy(:,1) = conv(BB,h_l,'same');
%         fy(:,2) = conv(BB,h_r,'same');
%
%         sound(fy,fs)
%         pause(2*dur)
%         % pause
%         hold off;
%     end
%
%
%     %     Nfft = length(h_l);
%     %     fsig = (1:Nfft)./Nfft.*fs;
% end
